/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package editor;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 *
 * @author emmanuel
 */
public class ToolBar extends JPanel
{
//	Vector tools;
	protected int separatorWidth = 8;
	protected int buttonWidth = 24;
	protected int buttonHeight = 24;
	protected int totalWidth = 0;
	
	public ToolBar() {
		super();
//		tools = new Vector(3);

		//setLayout(null);
                FlowLayout fl = new FlowLayout();
                fl.setAlignment(FlowLayout.LEFT);
                setLayout(fl);
		setBackground(Color.lightGray);
	}
	
	public int getSeparatorWidth() { return separatorWidth; }
	public void setSeparatorWidth(int w) { separatorWidth = w; }
	public int getButtonWidth() { return buttonWidth; }
	public void setButtonWidth(int w) { buttonWidth = w; }
	public int getButtonHeight() { return buttonHeight; }
	public void setButtonHeight(int h) { buttonHeight = h; }
	
	
	/**
	 * Adds an IconButton to this toolbar.
	 *
	 * @param imgurl The url of the image for the new IconButton
	 * @param al The ActionListener to add to the new IconButton
	 * @param id The id for the new button
	 */
	public void addButton(String imgurl, ActionListener al, int id) {
  	 ImageIcon ii = new ImageIcon(getClass().getResource(imgurl));
        Image i = ii.getImage();
        Image resizedImage = null;
 
        int iWidth = i.getWidth(null);
        int iHeight = i.getHeight(null);
 
        
            resizedImage = i.getScaledInstance(buttonWidth - 6, buttonHeight - 6, Image.SCALE_SMOOTH);
      
 
        // This code ensures that all the pixels in the image are loaded.
        ImageIcon temp = new ImageIcon(resizedImage);
 
            //Toolkit tk = Toolkit.getDefaultToolkit();
  	/*IconButton b = new IconButton(tk.getImage(imgurl).
  		getScaledInstance(buttonWidth - 6, buttonHeight - 6, Image.SCALE_SMOOTH), id);*/
        //new javax.swing.ImageIcon(getClass().getResource(imgurl))
        JButton b = new JButton(temp);
  	
        b.setBounds(totalWidth, 0, buttonWidth, buttonHeight);
  	b.addActionListener(al);
  	add(b);
  	
  	totalWidth += buttonWidth;
	}
	
	/** Adds a separator to this toolbar */
	public void addSeparator() {
		JPanel p = new JPanel(null);
		p.setSize(separatorWidth, buttonHeight);
		add(p);
		
		totalWidth += separatorWidth;
	}
	
	/** Adds a component to this toolbar */
	public void addComponent(Component c) {
		add(c);
		c.setLocation(totalWidth, 0);
		totalWidth += c.getWidth();
	}
	
	public void remComponent(Component c) {
		remove(c);
		
		totalWidth -= c.getWidth();
	}
}